<template>
  <div class="message-center-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              :icon="Plus"
              type="primary"
              style="margin-left: 20px"
              @click="handleEdit"
            >
              发消息
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane
        v-for="(item, index) in tabList"
        :key="index"
        :label="item.label"
        :name="item.value"
      >
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="消息ID"
            prop="NoticeID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="消息时间"
            prop="addTime"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="发送人"
            prop="FromStaff"
            show-overflow-tooltip
            width="120"
          />
          <el-table-column
            align="center"
            label="目标人"
            prop="ToStaffs"
            show-overflow-tooltip
            width="120"
          >
            <template #default="{ row }">
              <span
                v-for="(items, indexs) in row.ToStaffs"
                :key="indexs"
                style="margin: 0px 5px"
              >
                {{ items.StaffName }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            align="left"
            label="消息标题"
            prop="NoticeTitle"
            show-overflow-tooltip
            width="300"
          />
          <el-table-column label="消息内容">
            <template #default="{ row }">
              <div v-html="row.NoticeMemo"></div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button v-if="row.isRead" type="success" size="small">
                已读
              </el-button>
              <el-button
                v-else
                type="primary"
                size="small"
                @click="isRead(row)"
              >
                标记已读
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { Read } from '@/api/notice'
  import { getList, doDelete } from '@/api/messageCenter'
  import Edit from './components/MessageCenterEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'MessageCenter',
    components: { Edit },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tabList: [
          {
            label: '全部',
            value: 'all',
          },
          {
            label: '系统消息',
            value: 'system',
          },
          {
            label: '站内信',
            value: 'email',
          },
        ],
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          tab: 'all',
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }

      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })
      const isRead = async (row) => {
        console.log(row, '123123123')

        const dataMsg = {
          NoticeID: row.NoticeID,
        }
        Read(dataMsg)
      }

      return {
        ...toRefs(state),
        setSelectRows,
        isRead,
        handleTabClick,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
